/**
 * @Author: wangjian
 * 656876071@qq.com
 * @Date: 2024-08-02 17:00:37
 * @LastEditTime: 2024-09-11 14:06:39
 * @FilePath: src/core/common/hooks/TableHooks.ts
 * @Description:
 */
import {onMounted, type Reactive, ref} from "vue";
import type {Ret} from "@/core/utils/request";
import {message} from "ant-design-vue";
import type {LnMemberCase} from "@/views/sxln/api/LnMemberApi";

export const useQueryTable = (queryPageApi: Function, deleteByIdApi: Function, params: Reactive<any>, runDefaultMounted: boolean = true) => {
    const loading = ref(true)
    onMounted(() => {
        console.debug("执行mounted行为：", runDefaultMounted)
        if (runDefaultMounted)
            query()
    })

    /**
     * v2版本可以继续优化，不进行datasource赋值，传递Promise后续操作后再渲染
     */
    const query = (): Promise<Ret> => {
        loading.value = true
        console.debug("TableHooks：", params)
        return queryPageApi({
            params,
            current: pagination.value.current,
            size: pagination.value.pageSize
        }).then((res: any | Ret) => {
            console.debug("TableHooks：", res)
            if (res.data.records) {
                dataSource.value = res.data.records
                pagination.value.total = res.data.total
            }
            loading.value = false
            return res
        })
    }
    const dataSource = ref<LnMemberCase[]>()
    const pagination = ref({
        current: 1,
        pageSize: 10,
        total: 0,
        pageSizeOptions: ['10', '20', '30', '40'],
        showTotal: (total: number) => `共 ${total} 项数据`,
        showSizeChanger: true
    })
    const reset = () => {
        Object.keys(params).forEach(key => {
            params[key] = ''
        })
    }
    const handleTableChange = (page: any, filters: any, sorter: any, {action, currentDataSource}: any) => {
        if (action === 'paginate') {
            pagination.value.current = page.current
            pagination.value.pageSize = page.pageSize
            query()
        }
    }

    /**
     * 删除
     * @param id
     */
    const deleteSingle = (id: string) => {
        deleteByIdApi(id).then((res: any | Ret) => {
            if (res.code == 0) {
                message.success('删除成功')
                query()
            } else {
                message.error(res.data)
            }
        })
    }

    return {
        loading,
        query,
        reset,
        dataSource,
        pagination,
        handleTableChange,
        deleteSingle
    }
}

export const useQueryTableV1 = ({params, queryPageApi, deleteByIdApi, runDefaultMounted = true}: {
    params: Reactive<any>,
    queryPageApi: Function,
    deleteByIdApi: Function,
    runDefaultMounted?: boolean
}) => {
    const loading = ref(true)
    onMounted(() => {
        console.debug("执行mounted行为：", runDefaultMounted)
        if (runDefaultMounted)
            query()
    })

    /**
     * v2版本可以继续优化，不进行datasource赋值，传递Promise后续操作后再渲染
     */
    const query = (current: number = 1, size: number = pagination.value.pageSize): Promise<Ret> => {
        loading.value = true
        console.debug("TableHooks：", params)
        return queryPageApi({
            params,
            current,
            size
        }).then((res: any | Ret) => {
            console.debug("TableHooks：", res)
            if (res.data.records) {
                dataSource.value = res.data.records
                pagination.value.total = res.data.total
            }
            loading.value = false
            return res
        })
    }
    const dataSource = ref<LnMemberCase[]>()
    const pagination = ref({
        current: 1,
        pageSize: 10,
        total: 0,
        pageSizeOptions: ['10', '20', '30', '40'],
        showTotal: (total: number) => `共 ${total} 项数据`,
        showSizeChanger: true
    })
    const reset = () => {
        Object.keys(params).forEach(key => {
            params[key] = ''
        })
    }
    const handleTableChange = (page: any, filters: any, sorter: any, {action, currentDataSource}: any) => {
        if (action === 'paginate') {
            query(page.current, page.pageSize).then(
                res => {
                    pagination.value.current = page.current
                    pagination.value.pageSize = page.pageSize
                }
            )
        }
    }

    /**
     * 删除
     * @param id
     */
    const deleteSingle = (id: string) => {
        deleteByIdApi(id).then((res: any | Ret) => {
            if (res.code == 0) {
                message.success('删除成功')
                query()
            } else {
                message.error(res.data)
            }
        })
    }

    return {
        loading,
        query,
        reset,
        dataSource,
        pagination,
        handleTableChange,
        deleteSingle
    }
}